package cn.xej.easyexcel.export;

import cn.xej.easyexcel.common.BaseQuery;
import cn.xej.easyexcel.common.ExcelWriter;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.UUID;

/**
 * @author <a href="mailto:enjie.xu@yunhutech.com">xiaoming</a>
 * @since 2021/6/7
 */
public abstract class AbstractExportService<Q extends BaseQuery> implements IExportService<Q>{

    /**
     * 生成excel
     * 上传excel
     * @param clazz
     * @param data
     */
    protected String writeExcel(Class<?> clazz, List<?> data){
        return this.writeExcel(clazz,null,data);
    }

    protected String writeExcel(Class<?> clazz,List<List<String>> head, List<?> data) {
        return this.writeExcel(clazz,head,data,null);
    }

    protected String writeExcel(Class<?> clazz, List<List<String>> head, List<?> data, List<String> excludeFiled) {
        return this.writeExcel(clazz,head,this.title(),data,null);
    }

    private String writeExcel(Class<?> clazz, List<List<String>> head, String sheetName, List<?> data, List<String> excludeFiled) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ExcelWriter.writeExcel(bos, clazz, sheetName, head, excludeFiled, data);
        return this.uploadToOSS(bos);
    }

    private String uploadToOSS(ByteArrayOutputStream bos) {
        System.out.println("上传文件");
//        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
//        DirectUploadRequest uploadRequest = new DirectUploadRequest();
//        uploadRequest.setApplication("erpsaas-service");
//        uploadRequest.setFileType("xlsx");
//        uploadRequest.setGroupId(1002);
//        Result<UploadResponse<String>> uploadResult = OssServerClient.upload(uploadRequest, bis);
//        if (Result.isModelNull(uploadResult)) {
//            throw new ErpSaasException(uploadResult);
//        }
//        return uploadResult.getModel().getRelativePath();
        return "https://abc"+ UUID.randomUUID().toString().replaceAll("-","");
    }
}
